#include "TestEngine.h"
#include <cstdint>
#include "Logger.h"
#include "OnTestMsg.h"

using namespace std::placeholders;
using namespace xncc::user;
using namespace xncc::types;
TestEngine::TestEngine(thread_id_t tid) : EventEngine(tid)
{
}

void TestEngine::init()
{
    auto testFunc = std::bind(&TestEngine::handleTestMsg, this, _1, _2, _3, _4);
    registerUserFunc(1000, testFunc);
}

void TestEngine::handleTestMsg(const char* buf, uint32_t len, thread_id_t tid, connection_id_t connId)
{
    std::string rsp(buf, static_cast<std::size_t>(len));
    LOG_INFO(CONSOLE_LOG) << "receive server msg =" << rsp;
    sendData(rsp.c_str(), static_cast<uint32_t>(rsp.size()), 1000, tid, connId);
}

void TestEngine::handleDisconnected(connection_id_t msg, thread_id_t tid, const std::string& disconnectedReason)
{
    LOG_INFO(CONSOLE_LOG) << "TestEngine receive dis conn msg  peer addr =" << getPeerAddr(msg) << "connid = " << msg
                          << " reason = " << disconnectedReason;
}

void TestEngine::handleConnected(connection_id_t msg, thread_id_t tid)
{
    LOG_INFO(CONSOLE_LOG) << "TestEngine receive conn msg  peer addr = " << getPeerAddr(msg) << "connid = " << msg;
    std::string rsp = "100";
    sendData(rsp.c_str(), static_cast<uint32_t>(rsp.size()), 1000, tid, msg);
}